from SCons.Script import *

mode = ARGUMENTS.get('mode', 'release')
if not (mode in ['debug', 'release', 'profile']):
	print "Error: expected 'debug' or 'release' mode, found: " + mode
	Exit(1)

VariantDir(mode, '.', duplicate = 0)
yieldDir = mode + '/'

targets = 'all'

# Set the compiler flags for this mode.
flags = '-gdwarf-2';
if mode != 'debug':
	flags = '-O3'
if mode == 'profile':
	flags = ['-pg', flags]
flags = ['-Wall', flags]
Export('flags')

# Create the compilation environment
env = Environment()
env.Append(CCFLAGS = ['-I_source', flags])
if mode == 'profile':
	env.Append(LINKFLAGS = '-pg')


def Binary(name):
	lower = name.lower()
	out = yieldDir + 'bin/' + lower
	env.Alias(lower, out)
	binary = env.Program(out, Glob(yieldDir + name + '/*.cpp'))
	if lower in BUILD_TARGETS or 'all' in BUILD_TARGETS:
		Install(binary)


# Define which libraries we're using:
def Script(path):
	SConscript(path + '/SConscript', 
		variant_dir = mode + '/' + path,
		duplicate = 0)
	
	
def Using(module):
	env.Append(LIBPATH = [mode + '/_source/' + module])
	env.Append(LIBS = [module])
	Script('_source/' + module)


def Install(program):
	# Install the program.
	env.Install('/usr/local/bin', program)
	env.Alias('install', ['/usr/local/bin'])
	
	
def InstallResources(program, directory):
	# When doing an install build, create a hidden resource directory.
	resourceDir = os.environ['HOME'] + directory
	env.Alias('install', [resourceDir])

	# If we're doing an install build, change the write permissions of the
	# resource directory to 777.
	if 'install' in COMMAND_LINE_TARGETS:
		Command(resourceDir, program, [Mkdir(resourceDir), Chmod(resourceDir, 0777)])
	
	return resourceDir

